package net.wicp.tams.plugin.task.docker;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

import net.wicp.tams.common.Result;
import net.wicp.tams.common.apiext.IOUtil;
import net.wicp.tams.common.apiext.StringUtil;
import net.wicp.tams.common.others.docker.DockerUtil;
import net.wicp.tams.plugin.TaskAssit;

@Mojo(name = "dockbuild", requiresProject = false, threadSafe = false)
public class DockBuild extends AbstractMojo {
	// apiserver的url
	@Parameter(property = "url", defaultValue = "http://127.0.0.1:2375", required = false)
	private String url;
	// 要推送的image
	@Parameter(defaultValue = "${project.artifactId}:${project.version}", property = "image", required = false)
	private String image;
	// 有dockerfile文件的文件夹
	@Parameter(defaultValue = "${project.basedir}/", required = false)
	private String basedir;

	@Parameter(property = "registry", required = false)
	private String registry;

	@Override
	public void execute() throws MojoExecutionException, MojoFailureException {
		TaskAssit.initDockerUrl(url);
		if (StringUtil.isNotNull(registry)) {
			image = IOUtil.mergeFolderAndFilePath(registry, image);
		}
		Result ret = DockerUtil.getInst().buildImage(basedir, image);
		if (!ret.isSuc()) {
			throw new MojoFailureException("build docker失败：" + ret.getMessage());
		}
	}
}
